package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import javax.sql.DataSource;

//spring 配置类
@Configuration //编程一个配置类
@ComponentScan("com")
@EnableAspectJAutoProxy //代表开启aop
public class SpringConfig {

    //1.数据库连接信息
    @Bean
    public DataSource ds(){
        DataSource ds = new DruidDataSource();
        ((DruidDataSource) ds).setDriverClassName("com.mysql.jdbc.Driver");
        ((DruidDataSource) ds).setUrl("jdbc:mysql:///emp?useSSL=false&amp;characterEncoding=utf-8");
        ((DruidDataSource) ds).setUsername("root");
        ((DruidDataSource) ds).setPassword("root");
        return ds;
    }

    //2.配置sqlSession工程
    @Bean
    public SqlSessionFactoryBean ssfb(DataSource ds){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(ds);
        return ssfb;
    }

    //3.mapper接口得扫描器
    @Bean
    public MapperScannerConfigurer msc(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.mapper");
        return msc;
    }
}
